/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Component.h"

namespace gui
{

Component::Component(const Component* c) : m_focus(true), m_wasVisible(true), m_visible(true), m_listener(NULL), m_width(0), m_height(0), m_alpha(1.0f)
{
    SetOwner( c );
    m_bColor.Set( 1, 1, 1 );
    m_fColor.Set( 0 ,0, 0 );
    m_CanRemove = false;
}

Component::Component() : m_focus(true), m_wasVisible(true), m_visible(true),m_listener(NULL), m_width(0), m_height(0), m_alpha(1.0f)
{
    SetOwner( NULL );
    m_bColor.Set( 1, 1, 1 );
    m_fColor.Set( 0 ,0, 0 );
    m_CanRemove = false;
}

Component::~Component(void)
{
    type_ComponentList::iterator it = m_components.begin();
    for( ; it != m_components.end(); ++it )
    {
        delete *it;
    }
}

/** Remove a component */
void Component::RemoveComponent(gui::Component *c)
{
    type_ComponentList::iterator it = m_components.begin();
    for( ; it != m_components.end(); ++it )
    {
        if ( *it == c )
        {
            m_components.erase( it );
            delete *it;
            return;
        }
    }
}

/** Get a component */
const gui::Component* Component::GetComponent(gui::Component *c) const
{
    type_ComponentList::const_iterator it = m_components.begin();
    for( ; it != m_components.end(); ++it )
    {
        if ( *it == c )
        {
            return *it;
        }
    }
    return NULL;
}

/** Update this component */
void Component::Update( float dt )
{
    type_ComponentList::iterator it = m_components.begin();
    for( ; it != m_components.end(); ++it )
    {
        (*it)->Update( dt );
    }
}

void Component::Render( IRender* r )
{
    type_ComponentList::reverse_iterator it = m_components.rbegin();
    for( ; it != m_components.rend(); ++it )
    {
        if ( (*it)->IsVisible() )
            (*it)->Render( r );
    }
}


/** Handle input */
bool Component::HandleInput()
{
    type_ComponentList::const_iterator it = m_components.begin();
    for( ; it != m_components.end(); ++it )
    {
        if ( (*it)->HandleInput() )
            return true;
    }
    return false;

}
};